package com.saishi.dao.impl;

import com.saishi.dao.ExamDao;
import com.saishi.util.AliPool;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.poi.ss.formula.functions.Now;

import java.sql.*;

public class ExamDaoImpl implements ExamDao {

    //考试基本信息添加
    @Override
    public int addKao(String examname, String examstartdate, String examenddate, int examscore, int examtimelimit, String examdesc) throws SQLException {
        Connection conn= AliPool.getConn();
        String sql="insert into exam(examid,examname,examdesc,examscore,examtime_limit,examstartdate,examenddate,createtime) values(null,?,?,?,?,?,?,now())";
        PreparedStatement pst=conn.prepareStatement(sql);
        pst.setString(1,examname);
        pst.setString(2,examdesc);
        pst.setInt(3,examscore);
        pst.setInt(4,examtimelimit);
        pst.setString(5,examstartdate);
        pst.setString(6,examenddate);
        int num=pst.executeUpdate();
        return num;
    }

    @Override
    public int selectId(String examname, String examstartdate, String examenddate) throws SQLException {
        return 0;
    }

    QueryRunner qr = new QueryRunner(AliPool.getDs());
    @Override
    public String examque(int examid) throws SQLException {
        String sql ="select examquestionids from exam where examid=?";
        Connection conn  =AliPool.getConn();
        PreparedStatement pst = conn.prepareStatement(sql);
        pst.setInt(1,examid);
        ResultSet rs=pst.executeQuery();
        String ids =null;
        if (rs.next()){
            ids = rs.getString(1);//就算没取到它也会赋一个“”上去******************************************************************
        }
        pst.close();
        conn.close();
        return ids;
    }

    @Override
    public int update(String questios,int examid) throws SQLException {
        String sql = "update exam set examquestionids=? where examid=?";
        Connection conn  =AliPool.getConn();
        PreparedStatement pst = conn.prepareStatement(sql);
        pst.setString(1,questios);
        pst.setInt(2,examid);
        int or = pst.executeUpdate();
        pst.close();
        conn.close();
        return or;
    }

    @Override
    public int updasz(String examoptions, String examTriesLimit, String examlimit, String examanswer, int examid) throws SQLException {
        String sql = "update exam set examoptions=?,examTriesLimit=?,examlimit=?,examanswer=? where examid=?";
        Connection conn  =AliPool.getConn();
        PreparedStatement pst = conn.prepareStatement(sql);
        pst.setString(1,examoptions);
        pst.setString(2,examTriesLimit);
        pst.setString(3,examlimit);
        pst.setString(4,examanswer);
        pst.setInt(5,examid);
        int or = pst.executeUpdate();
        pst.close();
        conn.close();
        return or;
    }

    public static void main(String[] args) {
        ExamDao edao =new  ExamDaoImpl();
        try {// 结束时间分数考试时间1考试描述433
            int ok = edao.addKao("43434","2021-07-10T08:00","2021-07-10T10:00",400,1,"43");
            System.out.println(ok);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

//        try {
//            String s = edao.examque(1);
//            System.out.println("st:"+s);
//        } catch (SQLException throwables) {
//            throwables.printStackTrace();
//        }
//        try {
//            int ok = edao.update("1, 2, 3, 5",1);
//            System.out.println(ok);
//        } catch (SQLException throwables) {
//            throwables.printStackTrace();
//        }

    }

}
